package ru.evga.inse.server.gwt.admin.client.menu.dict.street;

import ru.evga.gwt.lib.common.client.domain.ResultContainer;
import ru.evga.gwt.lib.common.client.exception.ExceptionHandler;
import ru.evga.gwt.lib.common.client.request.IServiceRequestCallback;
import ru.evga.gwt.lib.common.client.request.RequestFactory;
import ru.evga.gwt.lib.common.client.request.params.FindEntityRangeParams;
import ru.evga.gwt.lib.common.client.ui.dialog.AbstractDtoEditDialog;
import ru.evga.gwt.lib.common.client.ui.editor.ISaveEntityCallback;
import ru.evga.inse.server.gwt.common.client.domain.dict.CityDto;
import ru.evga.inse.server.gwt.common.client.domain.dict.StreetDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;

public class StreetEditDialog extends AbstractDtoEditDialog<StreetDto, StreetEditPanel>
{

    interface Driver extends SimpleBeanEditorDriver<StreetDto, StreetEditPanel>{}
    Driver driver = GWT.create(Driver.class);

    private StreetEditPanel panel;

    public StreetEditDialog(StreetDto street, ISaveEntityCallback<StreetDto> persistCallback)
    {
        panel = new StreetEditPanel();
        fillCities(panel);
        init(street, panel, persistCallback, driver, "street");
    }
    
    protected Driver getDriver()
    {
        return driver;
    }
    
    private void fillCities(final StreetEditPanel panel)
    {
        FindEntityRangeParams params = FindEntityRangeParams.createDefaultParams();
        params.setEntityName("city");
        RequestFactory.findEntityRange(params, new IServiceRequestCallback<ResultContainer<CityDto>, String>()
        {

            public void ok(ResultContainer<CityDto> data)
            {
                panel.setCityPickerValues(data.getResultAsList());                
            }

            public void error(String data)
            {
                ExceptionHandler.handle(data);
            }
        });
        
    }
}
